// pages/apply/apply.js
var util = require("../../utils/utils.js");
var app = getApp()
Page({
  data: {
    orderNo: null
  },
  // form表单
  formSubmit: function(e) {
    let params = {
      productId: this.data.description.id,
      channel: 'WeiXin',
      formId: e.detail.formId,
      signRelation: e.detail.value.signRelation,
      emerMobile: e.detail.value.emerMobile,
      childName: e.detail.value.childName
    }
    if (!params.emerMobile) {
      wx.showToast({
        title: '联系电话不能为空',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    if (!params.childName) {
      wx.showToast({
        title: '小孩姓名不能为空',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    if (!params.signRelation) {
      wx.showToast({
        title: '请选择您与小孩的关系',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    this.pay(params);

  },
  formReset: function() {
    console.log('form发生了reset事件')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var _this = this
    // 头部数据接口
    wx.request({
      url: app.httpUrl + '/course/queryCourseDetail/' + options.productid,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        let description = res.data.data;
        description.startDate = util.formatDate(description.startDate, 'Y年M月D日');
        description.endDate = util.formatDate(description.endDate, 'Y年M月D日');
        _this.setData({
          description: description
        })
      }
    })
  },
  pay: function(params) {
    if (this.data.orderNo != null) {
      this.paySign(this.data.orderNo);
    } else {
      this.createSign(params);
    }
  },
  createSign: function(data) {
    let that = this;
    // 下单接口
    wx.request({
      url: app.httpUrl + '/course/order/createSign',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: data,
      method: "POST",
      success: (res) => {
        var signData = res.data.data;
        if (res.data.code != '000') {
          wx.showToast({
            title: '下单失败，请刷新再试',
            icon: 'none',
            duration: 2000
          });
          return;
        }
        that.setData({
          orderNo: signData.orderNo
        });
        this.payment(signData);
      }
    })
  },
  paySign: function(orderNo) {
    wx.request({
      url: app.httpUrl + '/pay/sign',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: {
        orderNo: orderNo
      },
      method: "POST",
      success: (res) => {
        var signData = res.data.data;
        this.payment(signData);
      }
    })
  },
  payment: function(signData) {
    var that = this;
    wx.requestPayment({
      'timeStamp': signData.timeStamp,
      'nonceStr': signData.nonceStr,
      'package': signData.packages,
      'signType': 'MD5',
      'paySign': signData.paySign,
      success: function(res) {
        that.setData({
          orderNo: null
        });
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        });

        wx.navigateTo({
          url: '/pages/orderDetail/orderDetail?orderNo=' + signData.orderNo,
          success: function() {
           console.log('success');
            this.onLoad();
          }
        })

      },
      fail: function(res) {
        wx.navigateTo({
          url: '/pages/orderDetail/orderDetail?orderNo=' + signData.orderNo,
          success: function () {
            
          }
        })
      },

      'complete': function(res) {
        console.log(res);
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})